package hh.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author huanghuai
 * @date 2020/9/29
 */
public class JdkProxyDemo implements InvocationHandler {

    static Animal animal = new Dog();

    public static void main(String[] args) {
        Animal proxyAnimal = (Animal) Proxy.newProxyInstance(
                JdkProxyDemo.class.getClassLoader(),
                new Class[]{Animal.class},
                new JdkProxyDemo());
        System.out.println(proxyAnimal.getClass().getName());
        proxyAnimal.eat();
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before...");
        Object invoke = method.invoke(animal, args);
        System.out.println("after ...");
        return invoke;
    }


}
